#include <php.h>
#include <wiringSerial.h>

#include "serial_library.h"

//serial-libary

PHP_FUNCTION(serialOpen)
{
    zend_string *device = NULL;
    long baud = 0;

    ZEND_PARSE_PARAMETERS_START(2, 2)
        Z_PARAM_STR(device)
        Z_PARAM_LONG(baud)
    ZEND_PARSE_PARAMETERS_END();

    long ret = serialOpen(ZSTR_VAL(device), baud);

    RETURN_LONG(ret);

}


//void serialClose (int fd) ;
PHP_FUNCTION(serialClose)
{
    long fd;
	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_LONG(fd)
	ZEND_PARSE_PARAMETERS_END();

    serialClose(fd);
}


//void  serialPutchar (int fd, unsigned char c) ;
PHP_FUNCTION(serialPutchar)
{
    long fd;
    long c;
	ZEND_PARSE_PARAMETERS_START(2, 2)
		Z_PARAM_LONG(fd)
		Z_PARAM_LONG(c)
	ZEND_PARSE_PARAMETERS_END();

    serialPutchar(fd, c);
}

//void  serialPuts (int fd, char *s) ;
PHP_FUNCTION(serialPuts)
{
    long fd;
    char* s = NULL;
    zend_string* str = NULL;

	ZEND_PARSE_PARAMETERS_START(2, 2)
		Z_PARAM_LONG(fd)
		Z_PARAM_STR(str)
	ZEND_PARSE_PARAMETERS_END();

    serialPuts(fd, ZSTR_VAL(str));

}

//void  serialPrintf (int fd, char *message, …) ;
PHP_FUNCTION(serialPrintf)
{

}

//int   serialDataAvail (int fd) ;
PHP_FUNCTION(serialDataAvail)
{
    long fd;
    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_LONG(fd)
    ZEND_PARSE_PARAMETERS_END();

    long ret = serialDataAvail(fd);
    RETURN_LONG(ret);
}

//int serialGetchar (int fd) ;
PHP_FUNCTION(serialGetchar)
{
    long fd;
    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_LONG(fd)
    ZEND_PARSE_PARAMETERS_END();

    long ret = serialGetchar(fd);

    RETURN_LONG(ret);
}



//void serialFlush (int fd) ;
PHP_FUNCTION(serialFlush)
{
    long fd;

    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_LONG(fd)
    ZEND_PARSE_PARAMETERS_END();

    serialFlush(fd);
}


//end for serial-library